<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PhotoController extends Controller
{
   //上传图片
   public function store(Request $request)
   {
      if ($request->hasFile('file') and $request->file('file')->isValid()) {

         //数据验证
         $allow = array('image/jpeg', 'image/png', 'image/gif');

         $mine = $request->file->getMimeType();
         if (!in_array($mine, $allow)) {
            return response()->json(['status' => 0, 'msg' => '文件类型错误，只能上传图片']);
         }

         //文件大小判断$filePath
         $max_size = 1024 * 1024 * 2;
         $size = $request->file->getClientSize();
         if ($size > $max_size) {
            return response()->json(['status' => 0, 'msg' => '文件大小不能超过2M']);
         }

         $date = date('Ymd');
         $path = $request->file->store('public/images/' . $date);

         //上传到本地要建立软连接将照片放到public目录去访问（执行命令 php artisan storage:link）
         return ['status' => 1, 'msg' => '/storage' . str_replace('public', '', $path)];
      }
   }

}


